package org.taobao.ibatiside.model.common.provider;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.emf.ecore.util.FeatureMap.Entry;
import org.eclipse.emf.ecore.xml.type.ProcessingInstruction;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.emf.edit.provider.FeatureMapEntryWrapperItemProvider;

public class IbatisFeaturemapProvider extends
		FeatureMapEntryWrapperItemProvider {

	public IbatisFeaturemapProvider(Entry entry, EObject owner,
			EAttribute attribute, int index, AdapterFactory adapterFactory,
			ResourceLocator resourceLocator) {
		super(entry, owner, attribute, index, adapterFactory, resourceLocator);
	}
	
	@Override
	public String getText(Object object) {
		String text = null;
	    if (getEntryFeature() == XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__TEXT)
	    {
//	      text = encode(getEntryValue().toString());
	      text = "SQL";
	    }
	    else if (getEntryFeature() == XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__CDATA)
	    {
	      text = "SQL[CDATA]";
	    }
	    else if (getEntryFeature() == XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__COMMENT)
	    {
	      text = "Comment";
	    }
	    else if (getEntryFeature() == XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__PROCESSING_INSTRUCTION)
	    {
	      ProcessingInstruction pi = (ProcessingInstruction)getEntryValue();
	      text = 
	        "<?" + 
	          (pi.getTarget() == null ? "" : encode(pi.getTarget())) + 
	          (pi.getData() != null && pi.getData().length() > 0 ? " " + encode(pi.getData()) + "?>" : "?>");
	    }
	    else if (ExtendedMetaData.INSTANCE.getFeatureKind(feature) == ExtendedMetaData.ATTRIBUTE_WILDCARD_FEATURE && isEntryAttribute())
	    {
	      text = getEntryFeature().getName() + "='" +
	        EcoreUtil.convertToString((EDataType)getEntryFeature().getEType(), getEntryValue()) + "'";
	    }
	    else if (getEntryValue() == null)
	    {
	      text = "<" + getEntryFeature().getName() + " xsi:nil=\"true\"/>";
	    }
	    else if (isEntryAttribute())
	    {
	      text = EcoreUtil.convertToString((EDataType)getEntryFeature().getEType(), getEntryValue());
	    }
	    else
	    {
	      text = super.getText(object);
	    }
	    return text;
	}
	
}
